package com.fly.lambda.framework.utils;

import java.io.*;
import java.util.List;

/**
 * Created by Fly on 2016/7/21.
 */
public class FileUtil {

    /**
     * 文件保存
     * @param targetPath    存储目录
     * @param fileName      文件名
     * @param is            文件流
     */
    public static void save(String targetPath, String fileName, InputStream is){
        File file = new File(targetPath);
        if (!file.exists()){
            //文件不存在，创建目录
            file.mkdirs();
        }

        try {
            FileOutputStream fos = new FileOutputStream(targetPath.concat(fileName));
            byte[] buffer = new byte[1024 * 1024];
            int bytesum = 0;
            int byteread = 0;
            while((byteread = is.read(buffer)) != -1){
                bytesum += byteread;
                fos.write(buffer, 0, byteread);
                fos.flush();
            }

            fos.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     * @param fileName  完整的文件路径
     * @return  删除是否成功，true: 成功， false: 失败
     */
    public static boolean delete(String fileName){
        boolean rtnValue = true;
        try {
            File file = new File(fileName);
            if (file.exists()) {
                file.delete();
            }
        }
        catch (Exception e){
            rtnValue = false;

        }
        return rtnValue;
    }

    public static String getExtension(String fileName){
        return fileName.substring(fileName.lastIndexOf("."), fileName.length());
    }

    /**
     * 文件重命名
     * @param oldFileName   需要修改的文件完整路径
     * @param newFileName   修改后的文件完整路径
     */
    public static void fileRename(String oldFileName, String newFileName){
        File oldFile = new File(oldFileName);
        File newFile = new File(newFileName);
        fileRename(oldFile, newFile);
    }

    public static void fileRename(File oldFile, File newFile){
        if (oldFile.exists()){
            oldFile.renameTo(newFile);
        }
    }

    public static void main(String[] args) throws Exception {
        File excelFile = new File("D:\\产品方案\\b2b\\importCourseWare_template.xls");
        if (excelFile.exists()){
            //文件存在
            List<List<Object>> data = ExcelUtils.readExcel(excelFile, 0);
            int count = 0;
            for(int i = 2; i < data.size(); i++){
                List<Object> row = data.get(i);
                String num = row.get(0).toString();
                String name = row.get(1).toString();
                String fileName = row.get(2).toString();

                File videoFile = new File("D:\\产品方案\\b2b\\video\\video\\" + fileName + ".mp4");
                boolean fileExist = videoFile.exists();
                if (fileExist){
                    File newFile = new File("D:\\产品方案\\b2b\\video\\video\\" + num + ".mp4");
                    fileRename(videoFile, newFile);
                    System.out.println(++count + ". " + num + ": " + name + ", rename success");
                }


            }
        }
    }
}
